<script setup>
import topNav from "../../components/topNav.vue";
import { onMounted, reactive, ref, getCurrentInstance, computed } from "vue";
import { onLoad, onReachBottom, onShow } from "@dcloudio/uni-app";
// import { appProductQuery, productCount } from "@/services/goods";
// import { purchaseQuery } from "@/services/delivery";
import { appPurchaseQuery } from "@/services/send";

// import tabbarView from "../components/tabbar.vue";
const scrollTop = ref(0);
const code = ref("");
const topHeight = ref(uni.getStorageSync("topHight") || 0);

onLoad((option) => {
  code.value = option.code ? option.code : "";
  form.value.keyword = option.code ? option.code : "";
  getAppProductQuery(1);
});
onShow(() => {});

const saveFun = () => {
  // console.log("取消");
  // form.value.name = "";
  // loading.value = "";
  // getAppProductQuery(1);
};

onReachBottom(() => {
  console.log("触底了");
  getAppProductQuery();
});

//获取商品
const form = ref({
  pageIndex: 1,
  pageSize: 20,

  keyword: "",
});

const productData = ref([]);

const loading = ref("");
const getAppProductQuery = async (index) => {
  if (index == 1) {
    productData.value = [];
    loading.value = "Loading";
  }
  if (index) {
    form.value.pageIndex = index;
  }
  if (loading.value == "noMore") {
    return;
  }
  loading.value = "Loading";
  form.value.deliverStatusS = [0, 5];
  try {
    let res = await appPurchaseQuery(form.value);
    if (res.code == 200) {
      productData.value = productData.value.concat(res.data.datas);

      form.value.pageIndex = form.value.pageIndex + 1;

      if (res.data.datas.length < form.value.pageSize) {
        loading.value = "noMore";
      } else {
        loading.value = "More";
      }
    } else {
    }

    console.log(res);
  } catch (e) {}
};

//编辑商品
const goEdt = (item) => {
  uni.navigateTo({ url: "/pages/sends/det/index?id=" + item.id });
};
//编辑商品

const ishowcamera = ref(false);
const scanFun = () => {
  console.log("扫码");
  // #ifdef H5
  uni.navigateTo({ url: "/pages/code_sand/index?type=goods" });
  // #endif
  // #ifdef APP-PLUS
  // 允许从相机和相册扫码

  const systemInfo = uni.getSystemInfoSync();
  if (systemInfo.osName === "android") {
    if (uni.getStorageSync("isspermission")) {
    } else {
      ishowcamera.value = true;
    }

    plus.android.requestPermissions(["android.permission.CAMERA"], (e) => {
      if (e.deniedAlways.length > 0) {
        // 弹出提示框解释为何需要权限，引导用户打开设置页面开启

        ishowcamera.value = false;
      } else if (e.deniedPresent.length > 0) {
        //权限被临时拒绝
        // 弹出提示框解释为何需要权限，可再次调用plus.android.requestPermissions申请权限
        ishowcamera.value = false;
      } else if (e.deniedAlways.length > 0) {
        //权限被永久拒绝
        // 弹出提示框解释为何需要权限，可再次调用plus.android.requestPermissions申请权限
        ishowcamera.value = false;
      } else {
        plus.android.requestPermissions(
          ["android.permission.READ_EXTERNAL_STORAGE"],
          (e) => {
            if (e.deniedAlways.length > 0) {
              // 弹出提示框解释为何需要权限，引导用户打开设置页面开启

              ishowcamera.value = false;
            } else if (e.deniedPresent.length > 0) {
              //权限被临时拒绝
              // 弹出提示框解释为何需要权限，可再次调用plus.android.requestPermissions申请权限

              ishowcamera.value = false;
            } else if (e.deniedAlways.length > 0) {
              //权限被永久拒绝
              // 弹出提示框解释为何需要权限，可再次调用plus.android.requestPermissions申请权限

              ishowcamera.value = false;
            } else {
              ishowcamera.value = false;
              uni.setStorageSync("isspermission", true);

              uni.scanCode({
                scanType: ["barCode", "qrCode"],
                autoDecodeCharset: true,
                success: function (res) {
                  console.log("条码类型：" + res.scanType);
                  console.log("条码内容：" + res.result);
                  form.value.keyword = res.result;
                  getAppProductQuery(1);
                },
              });
            }
          }
        );
      }
    });
    return;
  }
  uni.scanCode({
    scanType: ["barCode", "qrCode"],
    autoDecodeCharset: true,
    success: function (res) {
      console.log("条码类型：" + res.scanType);
      console.log("条码内容：" + res.result);
      form.value.keyword = res.result;
      getAppProductQuery(1);
    },
  });
  // #endif
};
const searchFun = () => {
  console.log("搜索");
  uni.navigateTo({
    url: "/pages/goods/goods_search/index",
  });
};
const goadd = () => {
  uni.navigateTo({
    url: "/pages/goods/goods_add/index?type=add",
  });
};

const searchlistfun = () => {};

const upper = (e) => {
  console.log(e);
};
const lower = (e) => {
  console.log(e);
};
const scroll = (e) => {
  console.log(e);
};
const sandfun = (item) => {
  uni.navigateTo({
    url: "/pages/delivery/send/index?id=" + item.id,
  });
};

const goSend = (item) => {
  uni.navigateTo({ url: "/pages/sends/sendDet/index?id=" + item.id });
};
//无网状态
const connected = () => {
  getAppProductQuery(1);
};
</script>

<template>
  <topNav
    title="新增收货单"
    @search="searchFun"
    @scan="scanFun"
    rType="save"
    :clickable="false"
  ></topNav>
  <view class="pay-top" :style="'top: calc( 88rpx + ' + topHeight + 'px);'">
    <view class="pay-search">
      <view class="pay-search-left">
        <up-search
          placeholder="配货单编号/供应商"
          v-model="keyword"
          @search="searchlistfun"
          :showAction="false"
          shape="square"
        ></up-search>
      </view>
    </view>
  </view>

  <view class="goods_form">
    <view
      class="scr_item"
      v-for="(item, index) in productData"
      :key="index"
      @click="goSend(item)"
    >
      <view class="item_bt_time" v-if="item.supplierName">
        <text>{{ item.supplierName }}</text>
      </view>
      <view class="item_title" @click.stop>
        <up-copy :content="item.code">
          <view class="item_title_left">
            <text>要货编号：{{ item.code }}</text>
            <image
              style="margin-left: 8rpx"
              src="@/static/images/copy.png"
              mode="scaleToFill"
            />
          </view>
        </up-copy>
      </view>

      <view class="item_content">
        <scroll-view
          class="scroll-view_H"
          scroll-x="true"
          @scroll="scroll"
          scroll-left="0"
        >
          <view
            id="demo1"
            class="scroll-view-item_H"
            v-for="(itemPr, indexPr) in item.detail"
            :key="index"
          >
            <image :src="itemPr.image" mode="scaleToFill" />
            <view class="scroll-view-name">{{ itemPr.productName }}</view>
            <view
              class="scroll-view-item_top"
              :class="itemPr.isStandard ? 'bp' : ''"
              >{{ itemPr.isStandard ? "标品" : "非标品" }}
            </view>
          </view>
          <!-- <view
            id="demo1"
            class="scroll-view-item_H"
           
          >
            <image
              src="https://cloud-test.yibaitao.com/74429e4c4ad3440e9d7dd8acf29cd5a8.png"
              mode="scaleToFill"
            />
            <view class="scroll-view-name">聚聚聚聚聚12312</view>
          </view> -->
        </scroll-view>

        <view class="item_content-right">
          <view>
            <view class="item_content-right-21">¥{{ item.totalAmount }} </view>
            <view class="item_content-right-22">{{ item.totalNum }}件</view>
          </view>
        </view>
      </view>
      <!-- <view class="item_bt_time" v-if="item.supplierName">
        供应商：<text>{{ item.supplierName }}</text>
      </view> -->

      <!-- <view class="item_bt_time"
        >日期：<text>{{ item.createTime }}</text>
      </view> -->
      <view class="bt_foods">
        <!-- v-if="item.deliverStatus == 0" -->
        <text>{{ item.createTime }}</text>

        <view class="bt_fh bt_qr" @click.stop="goSend(item)">去收货</view>
      </view>
    </view>
  </view>

  <!-- <view class="goods_form">
    <view
      class="scr_item"
      v-for="(item, index) in productData"
      :key="index"
      @click="goEdt(item)"
    >
      <view class="item_title" @click.stop>
        <view class="item_title_left">
          <text>{{ item.createTime }}</text>
          <view class="item_title_left-list">单据编号：{{ item.code }}</view>
          <view class="item_title_left-list2"
            >商品数量：{{ item.totalNum }}件,商品金额：¥{{
              item.totalAmount
            }}</view
          >
          <view class="item_title_left-list2"
            >供应商：{{ item.supplierName }}</view
          >
        </view>

        <text class="state" @click="goSend(item)">{{ "去收货" }}</text>
      </view>
    </view>
  </view> -->
  <view class="noData" v-if="productData.length <= 0 && loading == 'noMore'">
    <view class="noData-img">
      <image src="@/static/nodate/datas.png" mode="scaleToFill" />
      <view>暂无收货商品 </view>
    </view>
  </view>
  <view class="loading-form" v-if="loading && productData.length > 0"
    ><van-loading size="24rpx" v-if="loading == 'Loading'"
      ><text class="loading-text">加载中...</text>
    </van-loading>
    <text class="loading-text" v-if="loading == 'More'">加载更多</text>
    <text class="loading-text" v-if="loading == 'noMore'">~没有数据了~</text>
  </view>

  <!-- <view style="width: 100%; height: 100rpx"></view> -->

  <!-- <view class="goods_bt">
    <view class="goods_bt_to"
      ><up-button
        @click="goadd"
        class="goods_bt_to_btn"
        icon="plus"
        iconColor="#ffffff"
        text="新增收货单"
      ></up-button>
    </view>
  </view> -->

  <view class="show-camera" v-if="ishowcamera">
    <view class="show-camera-k">
      <view class="show-sm"> 设备权限使用说明 </view>
      <view class="show-des">
        为了给您带来更好的服务，需要获取您的相机权限与读取图片及文件权限，用于更换上传图片、扫一扫等功能，对于您授权的信息我们竭尽全力提供安全保护。
      </view>
    </view>
  </view>
  <noWork @connected="connected"> </noWork>
</template>

<style lang="scss" scoped>
@import "./index.scss";
</style>
